-- A sample score keeping display
-- This updates a display for a numeric score

-- Example usage:
--	Place the score at 50,50
-- 		score.init( { x = 50, y = 50 } )
--	Update the score to current value + 10:
--		score.setScore( score.getScore() + 10 )
module(..., package.seeall)

function updateScore(t, group, newScore)
    local theBackgroundBorder = 10

    local numbers = { 
        [string.byte("0")] = "images/digits/"..t.."0.png",
        [string.byte("1")] = "images/digits/"..t.."1.png",
        [string.byte("2")] = "images/digits/"..t.."2.png",
        [string.byte("3")] = "images/digits/"..t.."3.png",
        [string.byte("4")] = "images/digits/"..t.."4.png",
        [string.byte("5")] = "images/digits/"..t.."5.png",
        [string.byte("6")] = "images/digits/"..t.."6.png",
        [string.byte("7")] = "images/digits/"..t.."7.png",
        [string.byte("8")] = "images/digits/"..t.."8.png",
        [string.byte("9")] = "images/digits/"..t.."9.png",
    }

    group:remove(1)

    local numbersGroup = display.newGroup()
    group:insert( numbersGroup )
    local scoreStr = tostring( newScore )

    local scoreLen = string.len( scoreStr )
    local i = scoreLen	

    -- starting location is on the right. notice the digits will be centered on the background
    local x = group.contentWidth - theBackgroundBorder
    local y = group.contentHeight / 2
    
    while i > 0 do
        -- fetch the digit
        local c = string.byte( scoreStr, i )
        local digitPath = numbers[c]
        local characterImage = display.newImage( digitPath )

        -- put it in the score group
        numbersGroup:insert( characterImage )
        
        -- place the digit
        characterImage.x = x - characterImage.width / 2
        characterImage.y = y
        x = x - characterImage.width

        -- 
        i = i - 1
    end
end